package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;

import com.ym.medical.dao.SysHelpDao;
import com.ym.medical.domain.entity.SysHelpEntity;
import com.ym.medical.domain.param.SysHelpParam;
import com.ym.medical.enums.PublisStatusEnum;
import com.ym.medical.service.IConfSV;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ConfSVImpl extends BaseSVImpl<SysHelpEntity,SysHelpDao> implements IConfSV {



    @Override
    public SysHelpEntity insertSysHelp(SysHelpEntity sysHelp) {
        mapper.insertSelective(sysHelp);
        return sysHelp;
    }

    @Override
    public SysHelpEntity updateSysHelp(SysHelpParam sysHelp) {
        SysHelpEntity exitSysHelp = selectSysHelpById(sysHelp.getId());
        if(exitSysHelp == null){
            throw new BusinessException("该反馈记录不存在!");
        }
        mapper.updateByPrimaryKey(sysHelp.covert());
        return sysHelp.covert();
    }

    @Override
    public void deleteSysHelp(Long[] ids) {
        mapper.deleteByExample(TK.selectWhere(SysHelpEntity.class)
        .andIn(SysHelpEntity::getId,ids).end());
    }

    /**
     * 发布
     * @param ids
     * @return
     */
    @Override
    public void publishSysHelp(List<Long> ids) {
        if(ids.size()>0){
            for (Long id : ids) {
                SysHelpEntity exitSysHelp = selectSysHelpById(id);
                if(exitSysHelp == null){
                    throw new BusinessException("该反馈记录不存在!");
                }
                exitSysHelp.setReleaseState(PublisStatusEnum.OVER_PUBLISH.getKey());
                mapper.updateByPrimaryKeySelective(exitSysHelp);
            }
        }

    }

    /**
     * 撤回
     * @param ids
     */
    @Override
    public void recallSysHelp(List<Long> ids) {
        if(ids.size()>0){
            for (Long id : ids) {
                SysHelpEntity exitSysHelp = selectSysHelpById(id);
                if(exitSysHelp == null){
                    throw new BusinessException("该反馈记录不存在!");
                }
                exitSysHelp.setReleaseState(PublisStatusEnum.NO_PUBLISH.getKey());
                mapper.updateByPrimaryKeySelective(exitSysHelp);
            }
        }

    }

    /**
     * 帮助与反馈列表展示
     * @param sysHelp
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SysHelpEntity> listSysHelp(SysHelpParam sysHelp, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysHelpEntity> helpEntities = mapper.select(sysHelp.covert());
        return new PageInfo<>(helpEntities);
    }

    private SysHelpEntity selectSysHelpById(Long id) {
       return mapper.selectByPrimaryKey(id);
    }


}
